<template>
  <div class="exam-list-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>考试管理</span>
          <div class="header-actions">
            <el-button type="primary" @click="$router.push('/teacher/exams/create')">
              创建考试
            </el-button>
            <el-button type="success" @click="$router.push('/teacher/templates')">
              考试模板
            </el-button>
          </div>
        </div>
      </template>

      <!-- 搜索栏 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="考试名称">
          <el-input v-model="searchForm.name" placeholder="请输入考试名称" clearable />
        </el-form-item>
        <el-form-item label="考试状态">
          <el-select v-model="searchForm.status" clearable>
            <el-option label="全部" value="" />
            <el-option label="未开始" value="pending" />
            <el-option label="进行中" value="ongoing" />
            <el-option label="已结束" value="finished" />
          </el-select>
        </el-form-item>
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="searchForm.timeRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 考试列表 -->
      <el-table :data="examList" style="width: 100%">
        <el-table-column prop="name" label="考试名称" min-width="200" />
        <el-table-column prop="startTime" label="开始时间" width="180" />
        <el-table-column prop="endTime" label="结束时间" width="180" />
        <el-table-column prop="duration" label="考试时长" width="100">
          <template #default="scope">
            {{ scope.row.duration }}分钟
          </template>
        </el-table-column>
        <el-table-column prop="totalScore" label="总分" width="80" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280" fixed="right">
          <template #default="scope">
            <el-button-group>
              <el-button
                type="primary"
                size="small"
                :disabled="!canEdit(scope.row)"
                @click="handleEdit(scope.row)"
              >
                编辑
              </el-button>
              <el-button
                type="warning"
                size="small"
                :disabled="!canMonitor(scope.row)"
                @click="handleMonitor(scope.row)"
              >
                监控
              </el-button>
              <el-button
                type="success"
                size="small"
                @click="handleViewResults(scope.row)"
              >
                成绩
              </el-button>
              <el-button
                type="danger"
                size="small"
                @click="handleDelete(scope.row)"
              >
                删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()

// 搜索表单
const searchForm = reactive({
  name: '',
  status: '',
  timeRange: []
})

// 分页数据
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 考试列表数据
const examList = ref([
  {
    id: 1,
    name: 'C语言期中考试',
    startTime: '2024-04-01 14:00',
    endTime: '2024-04-01 16:00',
    duration: 120,
    totalScore: 100,
    status: '进行中'
  },
  {
    id: 2,
    name: 'C语言章节测试-指针',
    startTime: '2024-04-05 10:00',
    endTime: '2024-04-05 11:00',
    duration: 60,
    totalScore: 50,
    status: '未开始'
  },
  {
    id: 3,
    name: 'C语言实验考核',
    startTime: '2024-03-25 14:00',
    endTime: '2024-03-25 16:00',
    duration: 120,
    totalScore: 100,
    status: '已结束'
  }
])

const getStatusType = (status) => {
  const typeMap = {
    '未开始': 'info',
    '进行中': 'warning',
    '已结束': ''
  }
  return typeMap[status]
}

const canEdit = (exam) => {
  return exam.status === '未开始'
}

const canMonitor = (exam) => {
  return exam.status === '进行中'
}

const handleSearch = () => {
  // 实现搜索逻辑
  console.log('搜索条件：', searchForm)
}

const resetSearch = () => {
  searchForm.name = ''
  searchForm.status = ''
  searchForm.timeRange = []
  handleSearch()
}

const handleEdit = (exam) => {
  router.push(`/teacher/exams/edit/${exam.id}`)
}

const handleMonitor = (exam) => {
  router.push(`/teacher/monitor/${exam.id}`)
}

const handleViewResults = (exam) => {
  router.push(`/teacher/results/${exam.id}`)
}

const handleDelete = (exam) => {
  ElMessageBox.confirm(
    '确定要删除该考试吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 实现删除逻辑
    ElMessage.success('删除成功')
  }).catch(() => {})
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.exam-list-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 